<?php
namespace App\Http\Controllers\Web\Member\Buyer;

use App\Http\Controllers\Web\BaseController;
use Illuminate\Http\Request;

class PayController extends BaseController
{
    protected $api_url = [
        'pay_order_list' => ['get', 'buyer/orders/group_pay/list'],
        'find_by_orderid'=>['get','buyer/order_contract/find_by_orderid/%d'],
    ];

    protected $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    public function index()
    {
        $order_codes = $this->request->get('order_codes', '');
        //参数部分
        $params = [
            'order_codes' => $order_codes,
            'with' => 'orderProducts;invoice'
        ];

        //请求部分
        $ret = http_web($this->api_url['pay_order_list'])->send($params);

        $total_order_money = 0;
        if($ret['data']){
            if(isset($ret['data'][0]['order_type']) && $ret['data'][0]['order_type'] == 6){
                $result = http_web($this->api_url['find_by_orderid'],[$ret['data'][0]['id']])->send();
                if(isset($result['data'][0])){
                    if($result['data'][0]['status']!=1){
                        return view('web.order.czbank_pay_error', ['msg' => "还没有签合同不能支付"]);
                    }
                } 
            }
            $order_money = array_column($ret['data'], 'order_money');
            $total_order_money = array_sum($order_money);
        }else{
            //@todo 跳转至404
        }
//        dd($ret['data']);
        return view('web.order.submit_payment', ['orders' => $ret['data'], 'total_order_money' => $total_order_money, 'order_codes' => $order_codes]);
    }

    public function show()
    {

    }




    public function submit_payAction(Request $request)
    {
        $param = $request->all();
        $order_codes = isset($param['order_codes']) ? $param['order_codes'] : '';
        $total_order_money = isset($param['total_order_money']) ? $param['total_order_money'] : 0;
        $new_data = [
            'orderCodes' => $order_codes,
            'totalAmount' => number_format($total_order_money,2,".",""),
            'payChannel' => 0,
            'payAmount' => number_format($total_order_money,2,".",""),
        ];
    
        $url = '';
        switch ($param['pay_id']) {
            case '0':
                $url = config('url.czbank') . 'smartPay?' . http_build_query($new_data);
                break;

            default:
                break;
        }
        Header("Location: $url");
        die;
    }

    public function payError(Request $request){
        $msg = $request->input("msg","");
        return view('web.order.czbank_pay_error', ['msg' => $msg]);
    }



}